import api from "./init";
import {
    loginByEmailCodeParamsType,
    sendCodeParamsType,
    loginByEmailPasswordParamsType, LoginUserProfileType,
} from "@/types/login";
import {AxiosResponse} from "axios";
import {DeviceType} from "@/types/device";
import {Result} from "@/types/api-common";
import * as Device from "expo-device";

/**
 * @author BelovedLYC
 */
export const getEmailCodeApi = (
    data: sendCodeParamsType
): Promise<AxiosResponse<Result<void>>> => {

    return api.post("/email/send", data);
};

export const loginByEmailCodeApi = (
    data: loginByEmailCodeParamsType
): Promise<AxiosResponse<Result<LoginUserProfileType>>> => {
    return api.post("/user/login/email/code", data, {
        headers: {
            "X-Device-Id": Device.osInternalBuildId,
            "X-Device-Name": Device.deviceName,
            "X-Device-Type": DeviceType.PHONE
        }
    });
};

export const loginByEmailPassword = (
    data: loginByEmailPasswordParamsType
): Promise<AxiosResponse<Result<LoginUserProfileType>>> => {
    return api.post("/user/login/email/pass", data, {
        headers: {
            "X-Device-Id": Device.osInternalBuildId,
            "X-Device-Name": Device.deviceName,
            "X-Device-Type": DeviceType.PHONE
        }
    });
};


/**
 * 扫码登录API
 * @param qrId 二维码ID
 * @author FlanChan
 */
export const loginByQrCodeApi = (qrId: string): Promise<AxiosResponse<Result<void>>> => {
    return api.post(`/user/login/qrcode/scan/${qrId}`)
}

/**
 * 登出API
 * @author FlanChan
 */
export const logoutApi = (): Promise<AxiosResponse<Result<void>>> => {
    return api.delete("/user/login/logout",{
        headers: {
            "X-Device-Id": Device.osInternalBuildId,
        }
    });
};
